package com.example.community.list;

import com.example.community.dao.PaymanagerDao;
import com.example.community.dao.PaytypeDao;
import com.example.community.model.Paymanager;
import com.example.community.model.Paytype;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

@WebServlet(urlPatterns = "/userChargeServlet")
public class UserChargeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String usNo = req.getParameter("uid");
        PaytypeDao paytypeDao = new PaytypeDao();
        List<Paytype> paytypes = paytypeDao.fandByAll();
        req.setAttribute("paytypes",paytypes);
        req.setAttribute("usNo",usNo);

        req.getRequestDispatcher("/WEB-INF/userManager/charge.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String usNo = req.getParameter("usNo");
            String paytype = req.getParameter("paytype");
            String amount = req.getParameter("amount");

            Calendar calendar = Calendar.getInstance();

            Paymanager paymanager = new Paymanager();
            paymanager.setPtNo(Integer.parseInt(paytype));
            paymanager.setUsNo(Integer.parseInt(usNo));
            paymanager.setPmAmount(Double.valueOf(amount));
            paymanager.setPmOrderdt(calendar.getTime());
            paymanager.setPmState("未缴费");

            PaymanagerDao paymanagerDao = new PaymanagerDao();
            Integer insert = paymanagerDao.insert(paymanager);
            if (insert == 1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }catch (Exception e){
            resp.getWriter().write("false");
        }

    }
}
